package com.example.rentingapp.activity;

import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.cardview.widget.CardView;

import com.example.rentingapp.R;
import com.example.rentingapp.api.Api;
import com.example.rentingapp.api.ApiConfig;
import com.example.rentingapp.api.TtitCallback;
import com.example.rentingapp.enetity.InformationEntity;
import com.example.rentingapp.enetity.InformationResponse;
import com.example.rentingapp.fragment.MineFragment;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class MyHouseActivity extends BaseActivity {
    private CardView cvPublish, cvCheck;
    private ImageView ivBack;
    private List<InformationEntity> datas = new ArrayList<>();

    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    navigateTo(MyHouseCheckActivity.class);
                    break;
                case 1:
                    navigateTo(MyHousePublishActivity.class);
                    break;
            }
        }
    };

    @Override
    protected int initLayout() {
        return R.layout.activity_my_house;
    }

    @Override
    protected void initView() {
        cvPublish = findViewById(R.id.cv_publish_my_house);
        cvCheck = findViewById(R.id.cv_check_my_house);
        ivBack = findViewById(R.id.iv_back);
    }

    @Override
    protected void initData() {

        //点击前往发布房源界面
        cvPublish.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                searchPublishInformation();
            }
        });

        //点击前往查看房源界面
        cvCheck.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                searchInformation();
            }
        });

        //点击返回我的界面
        ivBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void searchInformation() {
        HashMap<String, Object> params = new HashMap<String, Object>();
        String informationUserId = findByKey("user_id");
        params.put("information_user_id", informationUserId);//从本地获取user_id
        Api.config(ApiConfig.FIND_INFORMATION, params).postRequest(mContext, new TtitCallback() {
            @Override
            public void onSuccess(String res) {
                Log.e("onSuccess", res);
                Gson gson = new Gson();
                InformationResponse response = gson.fromJson(res, InformationResponse.class);
                if (response.getSuccess() == true) {
                    List<InformationEntity> list = response.getDatas();
                    if (list != null && list.size() > 0) {
                        datas = list;
                        mHandler.sendEmptyMessage(0);
                    }
                } else {
                    showToastSync("个人信息未完善");
                }
            }

            @Override
            public void onFailure(Exception e) {
                Log.e("onFailure", e.toString());
            }
        });
    }

    private void searchPublishInformation() {
        HashMap<String, Object> params = new HashMap<String, Object>();
        String informationUserId = findByKey("user_id");
        params.put("information_user_id", informationUserId);//从本地获取user_id
        Api.config(ApiConfig.FIND_INFORMATION, params).postRequest(mContext, new TtitCallback() {
            @Override
            public void onSuccess(String res) {
                Log.e("onSuccess", res);
                Gson gson = new Gson();
                InformationResponse response = gson.fromJson(res, InformationResponse.class);
                if (response.getSuccess() == true) {
                    List<InformationEntity> list = response.getDatas();
                    if (list != null && list.size() > 0) {
                        datas = list;
                        mHandler.sendEmptyMessage(1);
                    }
                } else {
                    showToastSync("个人信息未完善");
                }
            }

            @Override
            public void onFailure(Exception e) {
                Log.e("onFailure", e.toString());
            }
        });
    }
}